package com.baway.biliclitent.fragment;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.FragmentZoneAdapter;
import com.baway.biliclitent.fragment.ZoneType.RegionTypeDetailsActivity;
import com.baway.biliclitent.fragment.ZoneType.ZoneLiveAppIndexActivity;
import com.baway.biliclitent.model.ZoneBean;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 分区 Fragment
 * 王培炎 完成：
 */

public class FragmentZone extends Fragment implements FragmentZoneAdapter.CheckOnClikc {

    private RecyclerView recycle;
    private ZoneBean zoneBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fargment_zone, container, false);
        recycle = (RecyclerView) view.findViewById(R.id.recycle_fragment_zone);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String s = readAssetsJson();
        getJson(s);
        loadData();

    }

    /**
     * 进行Json解析
     */
    private void getJson(String s) {
        Gson gson = new Gson();
        zoneBean = gson.fromJson(s, ZoneBean.class);
    }

    /**
     * 解析数据，进行适配
     */
    private void loadData() {
        recycle.setHasFixedSize(true);
        recycle.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        FragmentZoneAdapter zoneAdapter = new FragmentZoneAdapter(getActivity());
        recycle.setAdapter(zoneAdapter);
        //设置点击事件进行 个页面跳转
        zoneAdapter.setMyCheckOnClikc(this);
    }

    /**
     * 解析 assets 里面的json
     */
    private String readAssetsJson() {

        AssetManager assetManager = getActivity().getAssets();
        try {
            InputStream is = assetManager.open("region.json");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String str;
            while ((str = br.readLine()) != null) {
                stringBuilder.append(str);
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *    跳转方法
     */
    private void setIntent(int position){
        Intent intent = new Intent(getActivity(), RegionTypeDetailsActivity.class);
        ZoneBean.DataBeanHeadline dataBeanHeadline = zoneBean.getData().get(position);
        Bundle bundle = new Bundle();
        bundle.putInt("index",position);
        bundle.putSerializable("bean",dataBeanHeadline);
        intent.putExtras(bundle);
        startActivity(intent);
    }
    @Override
    public void getindex(int position) {
        switch (position) {
            case 0:
                Intent intent = new Intent(getActivity(), ZoneLiveAppIndexActivity.class);
                startActivity(intent);
                break;
            case 1:
                //番剧，进行页面跳转
                setIntent(position);
                break;
            case 2:
                //动画
                setIntent(position);
                break;
            case 3:
                //音乐
                setIntent(position);
                break;
            case 4:
                //舞蹈
                setIntent(position);
                break;
            case 5:
                //游戏
                setIntent(position);
                break;
            case 6:
                //科技
                setIntent(position);
                break;
            case 7:
                //生活
                setIntent(position);
                break;
            case 8:
                //鬼畜
                setIntent(position);
                break;
            case 9:
                //时尚
                setIntent(position);
                break;
            case 10:
                //广告
                Toast.makeText(getActivity(), "点点旁边！", Toast.LENGTH_SHORT).show();
                break;
            case 11:
                //娱乐
                setIntent(position);
                break;
            case 12:
                //电影
                setIntent(position);
                break;
            case 13:
                //电视剧
                Toast.makeText(getActivity(), "点点旁边！", Toast.LENGTH_SHORT).show();
                break;
            case 14:
                //游戏中心
                Toast.makeText(getActivity(), "点点旁边！", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
